/*
 * @Title Client.java
 * @Description
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @version 0.0.1
 */
package com.lizhen.rmi.client;

import com.lizhen.entity.Person;
import com.lizhen.util.HelloRMI;
import com.lizhen.util.HelloRMIJavaFX;
import com.lizhen.util.HelloRMIPerson;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.List;

/**
 * @author lizhen
 */
public class Client {

    /**
     * @author lizhen
     */
    public Client() {
    }

    /**
     * @author lizhen
     */
    public static void main(String[] args) {
//        HelloClient.main(null);
//        HelloClientJavaFX.main(null);
        HelloClientPerson.main(null);
    }

    static class HelloClient {
        public static void main(String[] args) {
            try {
                Registry registry = LocateRegistry.getRegistry(null);
                HelloRMI stub = (HelloRMI) registry.lookup("HelloRMI");

                stub.printMsg();
                System.out.println("Remote method invoked");
            } catch (AccessException e) {
                e.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (NotBoundException e) {
                e.printStackTrace();
            }
        }
    }

    static class HelloClientJavaFX {
        public static void main(String[] args) {
            try {
                Registry registry = LocateRegistry.getRegistry(null);
                HelloRMIJavaFX stub = (HelloRMIJavaFX) registry.lookup("HelloRMIJavaFX");
                stub.animation();

                System.out.println("Remote method invoked");
            } catch (AccessException e) {
                e.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (NotBoundException e) {
                e.printStackTrace();
            }
        }
    }

    static class HelloClientPerson {
        public static void main(String[] args) {
            try {
                Registry registry = LocateRegistry.getRegistry(null);
                HelloRMIPerson stub = (HelloRMIPerson) registry.lookup("HelloRMIPerson");
                List<Person> list = (List<Person>) stub.getPersons();
                for (Person s : list) {

                    System.out.println("name: " + s.getName());
                    System.out.println("age: " + s.getAge());
                }
                System.out.println("Remote method invoked");
            } catch (AccessException e) {
                e.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (NotBoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}